package com.tj.jinzao.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.tj.jinzao.BuildConfig;
import com.tj.jinzao.widgets.CustomWaitDialog;

import butterknife.ButterKnife;


/**
 * Fragment基类
 * Created by wjy on 2015/4/24.
 */
public class BaseFragment<V extends BasePresenter> extends Fragment implements View.OnClickListener {

    public static final String TAG = BuildConfig.TAG_PREFIX + "Fragment";

    public final static String ARG_LAYOUT_RES_ID = "layoutResId";

    protected int mLayoutResId;//布局文件

    protected FragmentActivity mContext;

    protected V mPresenter;

    protected V createPresenter() {
        return null;
    }

    public BaseFragment() {
        super();
    }
    public CustomWaitDialog progressDialog = null;

    public void startProgressDialog(Context context) {
        if (progressDialog == null) {
            progressDialog = new CustomWaitDialog(context);
        }
        progressDialog.show();
    }

    public void stopProgressDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = createPresenter();
        mContext = getActivity();
        Bundle arguments = getArguments();
        if (arguments != null) mLayoutResId = arguments.getInt(ARG_LAYOUT_RES_ID);
        if (mLayoutResId == 0) {
            mLayoutResId = getLayoutResId();
        }
    }
    public void displayImage(String url, ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .placeholder(com.yalantis.ucrop.R.color.ucrop_color_grey)
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        Glide.with(getActivity().getApplicationContext())
                .load(url)
                .transition(DrawableTransitionOptions.withCrossFade())
                .apply(options)
                .into(imageView);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (container == null) {
            // We have different layouts, and in one of them this
            // fragment's containing frame doesn't exist. The fragment
            // may still be created from its saved state, but there is
            // no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could
            // just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }
        View view = inflater.inflate(mLayoutResId, container, false);
        ButterKnife.bind(this, view);
        //If has title layout, set title
        setupViews(inflater, view);
        return view;
    }

    /**
     * 初始化view，可以重写此方法以避免重写onCreateView
     *
     * @param view
     */
    protected void setupViews(LayoutInflater inflater, View view) {

    }

    /**
     * 获取布局文件，如果外部参数没传，可以重写此方法
     */
    protected int getLayoutResId() {
        return 0;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter = null;
        }
    }

    @Override
    public Activity getContext() {
        return mContext;
    }

}
